﻿function Hmac(hashFunction, blockSize, key) {

    key = key.concat([]);
    if (key.length > blockSize) {
        key = hashFunction(key);
    } else {
        while (key.length < blockSize) {
            key.push(0);
        }
    }

    function xor(val) {
        var res = [];
        for (var i = 0; i < key.length; i++) {
            res[i] = key[i] ^ val;
        }
        return res;
    }

    var oKeyPad = xor(0x5c);
    var iKeyPad = xor(0x36);

    function encode(array) {
        return hashFunction(oKeyPad.concat(hashFunction(iKeyPad.concat(array))));
    }

    this.encode = encode;
}


function Sha256() {

    var self = this;

    function encode(array) {
        return sha256_digest(array);
    }

    self.encode = encode;
}

function HmacSha256(keyArray) {

    var self = this;

    var sha = new Sha256();
    var hmac = new Hmac(sha.encode, 64, keyArray);

    function encode(array) {
        return hmac.encode(array);
    }

    self.encode = encode;

}